/**
 * @author klv
 */
var QS_SearchOperateBar = function(container,config,callback){
	this.container=container;
	this.Untitled="Untitled";
	this.config={
		"separator":"|",
		"name":true
	};
	$.extend(this.config,config);
	this.data={
		"idr":{
			txt:"Investment Detail Report",
			visible:typeof this.config.idr!="undefined"?this.config.idr:true
		},
		"pdf":{
            txt:"Generate PDF",
			visible:typeof this.config.pdf!="undefined"?this.config.pdf:true
        },
		"default":{
            txt:"Set default",
			visible:typeof this.config["default"]!="undefined"?this.config["default"]:true
        },
		"new":{
            txt:"New",
			visible:typeof this.config["new"]!="undefined"?this.config["new"]:true
        },
		"open":{
            txt:this.config["openTxt"]||"Open",
			className:this.config.openClass||"qs-screener-extend",
			visible:typeof this.config.open!="undefined"?this.config.open:true
        },
		"save":{
			txt:"Save",
			visible:typeof this.config.save!="undefined"?this.config.save:true
		},
		"saveas":{
            txt:"Save as",
			visible:typeof this.config.saveas!="undefined"?this.config.saveas:true
        },
		"delete":{
            txt:"Delete",
			className:this.config.deleteClass||"qs-screener-extend",
			visible:typeof this.config["delete"]!="undefined"?this.config["delete"]:true
        },
		"clear":{
			txt:"Clear Form",
			visible:typeof this.config["clear"]!="undefined"?this.config["clear"]:false
		}
	};
	this.callback = callback;
	this.curOperator=null;
	this.init();
};
QS_SearchOperateBar.prototype={
	init:function(){
        this.el=$(this._genHtml()).appendTo(this.container);
		this.nameDom=this.el.find(".name");
		this.openDom=this.el.find(".open");
        this._bindOperEvent();
    },
	setName:function(name){
	    if(this.config.name){
			this.nameDom.text(name||this.Untitled);
		}else if(this.config.curNameOnOpen){
			this.openDom.text(name||this.config["openTxt"]);
		}
	},
	setButtonDisable:function(name,disable){
		if(!this.data[name]["visible"]) return;
		var button=this.el.find("."+name);
		if (button.hasClass("disabled")&&!disable) {
            button.removeClass("disabled");
        }else if (!button.hasClass("disabled")&&disable) {
            button.addClass("disabled");
		}
	},
	_bindOperEvent:function(){
        var self=this;
        this.el.bind("click.operateBar", function(e){
            //e.stopPropagation();
            if (e.target.tagName.toUpperCase() == "A") {
                var obj = $(e.target);
                var className = obj.attr("class");
                if (className.indexOf("disabled") != -1) return;
                if(className.indexOf(" ")!=-1){
                    className = className.substring(0,className.indexOf(" "));
                }
                self._trigger(className,[obj]);
                self._activeOperator(obj);
            }
        });
    },
	_activeOperator:function(obj){
		this.clearOn();
        this.curOperator=obj;
        this.curOperator.addClass('on');
	},
	clearOn:function(){
		if (this.curOperator) {
			this.curOperator.removeClass('on');
		}
	},
	_trigger:function(event, args){
        var callback = null;
        switch(event.toLowerCase()){
            case 'new':
                callback = this.callback.onNew;
            break;
            case 'open':
                 callback = this.callback.onOpen;
            break;
            case 'save':
                callback = this.callback.onSave;
            break;
            case 'saveas':
                callback = this.callback.onSaveas;
            break;
            case 'delete':
                callback = this.callback.onDelete;
            break;
            case 'pdf':
                callback = this.callback.onPDF;
            break;
            case 'idr':
                callback = this.callback.onIDR;
            break;
            case 'default':
                callback = this.callback.onDefault;
            break;
			case 'clear':
                callback = this.callback.onClear;
            break;
			default:
                callback = null;
			break;
        }
        if(callback){
            callback.apply(this, args);
        }
    },
	_genHtml:function(){
		var html='<div class="operateBar">',operate=null,className=null,tag=true;
		for(var data in this.data){
			operate=this.data[data];
			if(operate.visible){
				if(tag){tag=false;}else{html+=this.config.separator;}
				className=data;
				if(operate.className) className+=" "+operate.className;
				html+='<a class="'+className+'" href="javascript:void(0);">'+operate.txt+"</a>";
			}
		}
		html+="</div>";
		if(this.config.name){
			html+='<h3><span class="title">Name:</span><span class="name">'+this.Untitled+'</span></h3>';
		}
        return html; 
	}
};
